﻿using System;
using System.IO;
using System.Security.Principal;

namespace Fee.FileAttachs
{
    public class FileManager
    {
        private readonly string _folder;


        public FileManager(string folder)
        {
            _folder = folder;
            var path = Path.Combine(_folder, "Upload");
            if (!Directory.Exists(path)) Directory.CreateDirectory(path);
        }

        public string GetImportTemplateFile(string file)
        {
            var direct = Path.Combine(_folder, "Upload", "ImportTemplate");
            var fullfile = Path.Combine(direct, file);
            if (!File.Exists(fullfile)) throw new FileNotFoundException(fullfile + "并不存在");
            return fullfile;
        }

        public string GetTempFile()
        {
            var direct = Path.Combine(_folder, "Upload");
            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            direct = Path.Combine(direct, "temp");
            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            return Path.Combine(direct, Guid.NewGuid().ToString("N"));
        }

        public string GetPrivateTempFolder(IIdentity user)
        {
            var direct = Path.Combine(_folder, "Upload", user.Name);

            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            direct = Path.Combine(direct, "temp");
            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            return direct;
        }


        public string GetTempPrivateFile(string extension, IIdentity user)
        {
            var direct = GetPrivateTempFolder(user);
            var tempFileName = Guid.NewGuid().ToString("N") + extension;
            return Path.Combine(direct, tempFileName);
        }

        public string GetConstructionTaskFolder(int taskId)
        {
            var direct = Path.Combine(_folder, "Upload", "ConstructionTask");

            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            direct = Path.Combine(direct, taskId.ToString());
            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            return Path.Combine(direct, Guid.NewGuid().ToString("N"));
        }

        public string GetAuthProjectFileTask(string extentions, int authProjectOrderId)
        {
            var direct = Path.Combine(_folder, "Upload", "AuthProject");

            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            direct = Path.Combine(direct, authProjectOrderId.ToString());
            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            return Path.Combine(direct, Guid.NewGuid().ToString("N") + extentions);
        }

        public string GetBudgetOrderFileTask(string extentions, int budgetOrderid)
        {
            var direct = Path.Combine(_folder, "Upload", "AuthProject");

            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);
            direct = Path.Combine(direct, budgetOrderid.ToString());
            if (Directory.Exists(direct) == false) Directory.CreateDirectory(direct);

            return Path.Combine(direct, Guid.NewGuid().ToString("N") + extentions);
        }

    }
}